/**
 * created by zhouli on 2018/12/20
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import {paramsParse} from 'share/common';
//todo
//接口动作是否成功  需要给一个合理响应
//success error
// 不要返回200，什么都不提示，但是实际上后端没有创建成功

export default {
  getIfameUrl(url){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl +url)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //获取职能授权列表
  getRoleAuthorizationList(params) {
      // let params = {
      //     resourceId,  //职能权限id
      //     page,
      //     size,
      //     keyword
      // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/authorization/policy/list/resource', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 创建创建授权策略
  createAuthorizationItem(params) {
    //   let params = {
    //     "authorizationType": "other",           --------   全租户 all,所在公司 company,其他 other
    //     "tenantId":985801821654523905,
    //     "authorizationName":"所在公司",
    //     "authorizationCode":"所在公司"
    //     "enabled": true,
    //      "resourcePermissionId":1077167530856054786,
    //
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/authorization/policy', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //修改授权策略
  updateAuthorizationItem(params) {
    //   let params = {
    //     "authorizationType": "other",           --------   全租户 all,所在公司 company,其他 other
    //     "tenantId":985801821654523905,
    //     "authorizationName":"所在公司",
    //     "authorizationCode":"所在公司"
    //     "enabled": true,
    //      "resourcePermissionId":1077167530856054786,
    //
    // }
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/authorization/policy', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //查询授权策略详情
  getAuthorizationItemDetail(params) {
    // let params = {
    //   authId
    // }
    //返回结果如下
    // let res = {
    //   "authorizationType": "all",
    //   "tenantId": "985801821654523905",
    //   "authorizationName": "授权策略1",
    //   "authorizationCode": "auth1",
    //   "enabled": true,
    //   "id": "1078536016798830593",
    //   "resourcePermissionId": "1077167530856054786",
    //   "orgPermissionId":"1077177895258062850"
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/authorization/policy', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })

  },
  //针对财务角色，开启或者关闭数据权限
  switchDataPermission(params){
    // let params = {
    //   resourcePermissionId,
    //   enableDataPermission
    // }
    //todo
    //后端最好优化接口
    let _params = paramsParse(params);
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/security/permission/resource/data/switch'+_params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  }
}
